<!DOCTYPE html>
<html lang="en-US">
  <head>
    <link href="/assets/index.css" rel="stylesheet" type="text/css" />
    <script crossorigin="anonymous" src="/test-harness.js"></script>
    <script crossorigin="anonymous" src="/test-page-object.js"></script>
    <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
  </head>
  <body>
    <main id="webchat"></main>
    <script>
      run(async function () {
        function createDirectLineForTest(options) {
          const UNINITIALIZED = 0;
          const workingDirectLine = window.WebChat.createDirectLine(options);

          return {
            ...workingDirectLine,
            activity$: new Observable(observer => {
              const subscription = workingDirectLine.activity$.subscribe({
                complete() {
                  observer.complete();
                },
                error(error) {
                  observer.error(error);
                },
                next(value) {
                  // Fill as much null as possible, it should not break rendering.
                  value = [
                    'attachmentLayout',
                    'attachments',
                    'channelData',
                    'conversation',
                    'entities',
                    'from',
                    'inputHint',
                    'locale',
                    'name',
                    'recipient',
                    'speak',
                    'suggestedActions',
                    'text',
                    'textFormat',
                    'timestamp',
                    'type'
                  ].reduce((activity, name) => updateIn(activity, [name], value => value || null), value);

                  observer.next(value);
                }
              });

              return () => subscription.unsubscribe();
            }),
            postActivity: workingDirectLine.postActivity.bind(workingDirectLine),
            connectionStatus$: workingDirectLine.connectionStatus$
          };
        }

        WebChat.renderWebChat(
          {
            directLine: createDirectLineForTest({ token: await testHelpers.token.fetchDirectLineToken() }),
            store: testHelpers.createStore()
          },
          document.getElementById('webchat')
        );

        await pageConditions.uiConnected();

        await pageObjects.sendMessageViaSendBox('echo Hello, World!', { waitForSend: true });
        await pageConditions.minNumActivitiesShown(3);

        await pageObjects.sendMessageViaSendBox('suggested-actions', { waitForSend: true });
        await pageConditions.minNumActivitiesShown(5);
        await pageConditions.suggestedActionsShown();
        await pageConditions.allImagesLoaded();

        await host.snapshot('local');
      });
    </script>
  </body>
</html>
